//
//  SFShouCangViewController.m
//  GiftThat
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFShouCangViewController.h"
#import "SFSelectionCellModel.h"
#import "SFShoucangTableViewCell.h"
#import "SFJumpWebViewController.h"
@interface SFShouCangViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *titleArray;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) NSArray *array;
@end

@implementation SFShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self GataTableView];
}
-(void)GataTableView{
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 110) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;

    [self.tableview registerClass:[SFShoucangTableViewCell class] forCellReuseIdentifier:@"UIShouCangableViewCellIdentifier"];
    [self.view addSubview:self.tableview];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:YES];
    [self.tableview setEditing:editing animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];
    self.array = [store getAllItemsFromTable:@"like"];
  
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SFShoucangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UIShouCangableViewCellIdentifier"];
    NSMutableArray *mutablearray = [NSMutableArray array];
    NSMutableArray *mutableTitleArray = [NSMutableArray array];
    for (SAKeyValueItem *items in self.array) {
        SFSelectionCellModel *SelectionCellModel = [[SFSelectionCellModel alloc] initWithDictionary:items.itemObject];
        [mutableTitleArray addObject:items.itemId];
    
        [mutablearray addObject:SelectionCellModel];
    }
    self.titleArray = [[NSMutableArray alloc]initWithArray:mutableTitleArray];
    self.dataSource = [[NSMutableArray alloc]initWithArray:mutablearray];
    [cell setSelectionCellModel:self.dataSource[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return self.view.height / 4;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *url = [NSString stringWithFormat:@"%@",[self.dataSource [indexPath.row]url]];
 
    SFJumpWebViewController *JumpWebViewController = [[SFJumpWebViewController alloc]initWithURL:url];
    [self.navigationController pushViewController:JumpWebViewController animated:YES];
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"Refresh" object:nil userInfo:nil];
    [self.dataSource removeObjectAtIndex:indexPath.row];
         

    SAKeyValueStore *store = [[SAKeyValueStore alloc] initDBWithName:@"SFstore.db"];
    
    [store deleteObjectById:self.titleArray[indexPath.row] fromTable:@"like"];
    
    [store close];
    [tableView reloadData];

    
    
}


@end
